
import {PlayerPost} from "./PlayerPost";
import {Player} from "./base/Player";
import {PlayerData} from "./PlayerData";
import {TableData} from "./TableData";

export class ReplayMgr{
    private static proto:Array<any>;
    private static playIndex;//当前播放第几局
    private static protoIndex;
    private static playSpeed;//播放速度
    private static count;
    private static playHandler;
    private static autoNextRound:boolean;
    private static isRuning = false;
    static start(proto,playIndex?){
        ReplayMgr.isRuning = true;
        cc.director.loadScene("TexasScene");
        console.log(proto);
        ReplayMgr.proto = proto;
        ReplayMgr.playSpeed = 1;
        if(playIndex == null){
            ReplayMgr.playIndex = 0;
            ReplayMgr.autoNextRound = true;
        }else{
            ReplayMgr.playIndex = playIndex;
            ReplayMgr.autoNextRound = false;
        }
        ReplayMgr.protoIndex = 0;
        ReplayMgr.play();
    }

    static exit(){
        ReplayMgr.isRuning = false;
        clearInterval(ReplayMgr.playHandler);
    }
    static isReplay(){
        return ReplayMgr.isRuning;
    }
    //减速
    static setSlow(){
        if(ReplayMgr.playSpeed == 16) return false;
        ReplayMgr.playSpeed = ReplayMgr.playSpeed * 2;
        return true;
    }

    //加速
    static setFast():boolean{
        if(ReplayMgr.playSpeed == 1) return false;
        ReplayMgr.playSpeed = ReplayMgr.playSpeed / 2;
        return true;
    }

    //下一局
    static nextRound():boolean{
        if(ReplayMgr.playIndex == ReplayMgr.proto.length-1) return false;
        cc.director.loadScene("TexasScene");
        ReplayMgr.playIndex++;
        ReplayMgr.protoIndex = 0;
        ReplayMgr.play();
        return true;
    }

    //上一局
    static lastRound():boolean{
        if(ReplayMgr.playIndex == 0) return false;
        cc.director.loadScene("TexasScene");
        ReplayMgr.playIndex--;
        ReplayMgr.protoIndex = 0;
        ReplayMgr.play();
        return true;
    }

    static play(){
        ReplayMgr.count = 0;
        clearInterval(ReplayMgr.playHandler);
        ReplayMgr.playHandler = setInterval(function () {
            ReplayMgr.count ++ ;
            if(ReplayMgr.count % ReplayMgr.playSpeed == 0){
                ReplayMgr.nextProto();
            }
        },250);
    }

    static pause(){
        clearInterval(ReplayMgr.playHandler);
    }

    static getPlaySpeed(){
        return ReplayMgr.playSpeed
    }

    private static nextProto(){
        let i = ReplayMgr.playIndex;
        let j = ReplayMgr.protoIndex;
        if(j>=ReplayMgr.proto[i].length && !ReplayMgr.autoNextRound){
            return;
        }else if(j>=ReplayMgr.proto[i].length && ReplayMgr.autoNextRound){
            ReplayMgr.nextRound()
            return;
        }
        let p = ReplayMgr.proto[i][j];
        ReplayMgr.protoIndex++;
        ReplayMgr.dealMsg(p.event,p.data);
    }

    private static dealMsg(event,data){
        console.log("replay ",event,data);
        if(event == "table_info"){
            PlayerPost.onReceiveMessage(event,data);
            ReplayMgr.nextProto();
        }else if(event == "player_info"){
            let playerInfo = data;
            let player = new Player();
            player.userid = playerInfo.user_info.userid;
            player.nickname = playerInfo.user_info.nickname;
            player.sex = playerInfo.user_info.sex;
            player.headimg = playerInfo.user_info.headimg;
            player.gold = playerInfo.user_info.gold;
            player.bodygold = playerInfo.user_info.bodygold;
            player.diamond = playerInfo.user_info.diamond;
            player.level = playerInfo.user_info.level;
            player.seat = playerInfo.player_status.seatid;
            player.status = playerInfo.player_status.status;
            PlayerData.addPlayer(player);
            ReplayMgr.nextProto();
        }else if(event == "player_info_finish"){
            for(let seat = 0 ;seat<TableData.seatcount;seat++){
                let player = PlayerData.getPlayerByServerSeat(seat);
                if(player == null) continue;
                let localSeat = PlayerData.serverSeatToLocalSeat(seat);
                console.log(localSeat,player);
                PlayerPost.view.onPlayerEnter(localSeat,player);
            }
            ReplayMgr.nextProto();
        }else if(event == "game_msg"){
            PlayerPost.view.onReceiveMessage(data);
        }else{
            PlayerPost.onReceiveMessage(event,data);
        }
    }
}